package class01;

/**
 * @description: 冒泡排序
 * @time: 2021/7/2 15:57
 * @author: lhy
 */
public class BubbleSort {
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    public static void bubbleSort(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int end = arr.length-1; end >= 0; end--){
            for (int i = 0; i < end; i++) {
                if (arr[i] > arr[i+1]) {
                    swap(arr,i,i+1);
                }
            }
        }
    }

    public static void printArray(int[] arr){
        for (int value : arr) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int[] arr = {1,4,9,2,4,6,2,45,33};
        printArray(arr);
        bubbleSort(arr);
        printArray(arr);
        System.out.println("test");
    }
}
